package com.example.aigc_education.controller;

import com.example.aigc_education.anno.RequestKeyParam;
import com.example.aigc_education.anno.RequestLock;
import com.example.aigc_education.common.BusinessException;
import com.example.aigc_education.domain.request.AddFlashCardsRequest;
import com.example.aigc_education.domain.request.AddQuizzesRequest;
import com.example.aigc_education.domain.request.SaveFlashCardListRequest;
import com.example.aigc_education.domain.vo.BriefFlashCardListVO;
import com.example.aigc_education.domain.vo.EntireFlashCardListVO;
import com.example.aigc_education.service.PaperService;
import com.example.aigc_education.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/flashCard")
public class FlashCardController {

    @Resource
    private PaperService paperService;

    /**
     * 展示该主题下的抽认卡集合列表
     * @param themeId 主题id
     * @return 简单抽认卡信息集合列表
     */
    @GetMapping("{themeId}/getBriefList")
    public ResultUtil showAllFlashCardList(@NotNull(message = "主题id不能为空") @PathVariable Long themeId){
        List<BriefFlashCardListVO> briefFlashCardListVOS =  paperService.showAllFlashCardList(themeId);

        return ResultUtil.success(briefFlashCardListVOS);
    }


    /**
     * 通过抽认卡集id 展示一个完整的抽认卡集合，里面有抽认卡
     * @param flashCardListId 抽认卡集id
     * @return 一个完整的抽认卡集合
     */
    @GetMapping("/{flashCardListId}/getEntireOne")
    public ResultUtil getEntireFlashCardList(@NotNull(message = "抽认卡id不能为空") @PathVariable Long flashCardListId){
        EntireFlashCardListVO entireFlashCardListVO =  paperService.getEntireFlashCardList(flashCardListId);
        return ResultUtil.success(entireFlashCardListVO);
    }


    @RequestLock(prefix = "saveFlashCardList:",expire = 3)
    @PutMapping("/save")
    public ResultUtil saveFlashCardList(@RequestKeyParam @RequestBody @Validated SaveFlashCardListRequest saveFlashCardListRequest){
        boolean success = paperService.submitFlashCardList(saveFlashCardListRequest);
        if (!success) {
            throw new BusinessException("操作失败");
        }
        return ResultUtil.success("操作成功");
    }

    /**
     * 删除计划
     * @param flashCardListId 抽认卡集id
     * @return 返回删除抽认卡结果
     */
    @RequestLock(prefix = "deleteFlashCardList:",expire = 3)
    @PutMapping("/{flashCardListId}/delete")
    public ResultUtil deleteFlashCardList(@RequestKeyParam @PathVariable @NotNull(message = "抽认卡id不能为空") Long flashCardListId){
        boolean removed = paperService.deleteFlashCardList(flashCardListId);
        if (!removed) {
            throw new BusinessException("删除失败");
        }
        return ResultUtil.success("删除成功");
    }


    @RequestLock(prefix = "batchSaveFlashCards:",expire = 3)
    @PostMapping("/batch/save")
    public ResultUtil batchSaveQuiz(@RequestKeyParam @Validated @RequestBody AddFlashCardsRequest request){
        boolean success = paperService.batchSaveFlashCards(request);
        if (!success) {
            throw new BusinessException("保存失败");
        }
        return ResultUtil.success("保存成功");
    }

}
